package com.mercury.coffee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.coffee.entity.CoffeeCommentInfo;
import com.mercury.coffee.entity.PushCoffee;
import com.mercury.coffee.entity.PushCoffeeComments;
import com.mercury.coffee.mapper.PushCoffeeCommentsMapper;
import com.mercury.coffee.service.PushCoffeeCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.coffee.service.PushCoffeeService;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.service.CenterUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 童雅婷
 * @since 2021-11-21
 */
@Service
public class PushCoffeeCommentsServiceImpl extends ServiceImpl<PushCoffeeCommentsMapper, PushCoffeeComments> implements PushCoffeeCommentsService {

    @Autowired
    private CenterUserService userService;
    @Autowired
    private PushCoffeeService coffeeService;

    @Override
    public List<CoffeeCommentInfo> getCommentByCoffee(int id) {
        List<CoffeeCommentInfo> list = new ArrayList<>();
        QueryWrapper<PushCoffeeComments> wrapper = new QueryWrapper<>();
        wrapper.eq("coffee_id",id);
        List<PushCoffeeComments> pushCoffeeComments = baseMapper.selectList(wrapper);
        for (PushCoffeeComments pushCoffeeComment : pushCoffeeComments) {
            CoffeeCommentInfo coffeeCommentInfo = new CoffeeCommentInfo();
            BeanUtils.copyProperties(pushCoffeeComment,coffeeCommentInfo);
            CenterUser user = userService.getById(pushCoffeeComment.getUserId());
            BeanUtils.copyProperties(user, coffeeCommentInfo);
            list.add(coffeeCommentInfo);
        }
        return list;

    }

    @Override
    public List<CoffeeCommentInfo> getCommentByUser(String id) {
        List<CoffeeCommentInfo> list = new ArrayList<>();
        QueryWrapper<PushCoffeeComments> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<PushCoffeeComments> pushCoffeeComments = baseMapper.selectList(wrapper);
        for (PushCoffeeComments pushCoffeeComment : pushCoffeeComments) {
            CoffeeCommentInfo coffeeCommentInfo = new CoffeeCommentInfo();
            BeanUtils.copyProperties(pushCoffeeComment,coffeeCommentInfo);
            PushCoffee coffee = coffeeService.getById(pushCoffeeComment.getCoffeeId());
            BeanUtils.copyProperties(coffee, coffeeCommentInfo);
            list.add(coffeeCommentInfo);
        }
        return list;
    }
}
